package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.core.util.PageUtil;
import com.hbnu.system.mapper.TeamMapper;
import com.hbnu.system.model.dto.SearchPageDTO;
import com.hbnu.system.model.entity.Team;
import com.hbnu.system.service.ITeamService;
import com.hbnu.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    @Autowired
    private ITeamService iTeamService;

    @Autowired private IUserService iUserService;

    @Override
    public boolean addTeam(Team team) {
        if(StrUtil.isEmpty(team.getClassifi()) || StrUtil.isBlank(team.getClassifi())){
            throw new BaseException("请输入统计分类!");
        }
        if(StrUtil.isEmpty(team.getTeamName()) || StrUtil.isBlank(team.getTeamName())){
            throw new BaseException("请输入团队名称!");
        }
        if(StrUtil.isEmpty(team.getTeamHead()) || StrUtil.isBlank(team.getTeamHead())){
            throw new BaseException("请输入团队带头人!");
        }

        team.setEnteredDate(LocalDateTime.now());
        return baseMapper.insert(team) == 1;
    }

    @Override
    public boolean deleteTeam(String teamID) {
        Team one = super.getOne(Wrappers.lambdaQuery(Team.class)
                .eq(Team::getTeamID, teamID));
        if (Objects.isNull(one)){
            throw new BaseException("科研团队编号有误!");
        }
        return baseMapper.deleteById(teamID) == 1;
    }

    @Override
    public boolean updateTeam(Team team) {
//        if (team.getStatus().equals("待校级审核")){
//            throw new BaseException("审核中不可修改!");
//        }
        if(StrUtil.isEmpty(team.getClassifi()) || StrUtil.isBlank(team.getClassifi())){
            throw new BaseException("请输入统计分类!");
        }
        if(StrUtil.isEmpty(team.getTeamName()) || StrUtil.isBlank(team.getTeamName())){
            throw new BaseException("请输入团队名称!");
        }
        if(StrUtil.isEmpty(team.getTeamHead()) || StrUtil.isBlank(team.getTeamHead())){
            throw new BaseException("请输入团队带头人!");
        }

        return baseMapper.updateById(team) == 1;
    }

    @Override
    public Team getTeamById(String teamID) {
        Team team = super.getOne(Wrappers.lambdaQuery(Team.class)
                .eq(Team::getTeamID, teamID));
        if(Objects.isNull(team)){
            throw new BaseException("团队编号异常!未查找到该科研团队");
        }
        return team;
    }

    @Override
    public Team getTeamByTeamNum(String teamNum) {
        Team team = super.getOne(Wrappers.lambdaQuery(Team.class)
                .eq(Team::getTeamNum, teamNum));
        if(Objects.isNull(team)){
            throw new BaseException("团队编号异常!未查找到该科研团队");
        }
        return team;
    }

    @Override
    public Team getTeamByTeamName(String teamName) {
        Team team = super.getOne(Wrappers.lambdaQuery(Team.class)
                .eq(Team::getTeamName, teamName));
        if(Objects.isNull(team)){
            throw new BaseException("团队名称异常!未查找到该科研团队");
        }
        return team;
    }

    @Override
    public IPage<Team> pageCondition(SearchPageDTO searchPageDTO) {
        IPage<Team> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
        LambdaQueryWrapper<Team> wrapper = Wrappers.lambdaQuery(Team.class);
        IPage<Team> teamIPage = baseMapper.selectPage(page, wrapper);

        List<Team> records = teamIPage.getRecords();
        List<Team> TeamS = new ArrayList<>();

        int count = 1;
        int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum()-1)+1;
        int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
        Collections.reverse(records);
        for (Team record:records) {
            if (count >= start && count <= end){
                TeamS.add(this.getTeamById(record.getTeamID()));
            }
            count += 1;
        }

//        Collections.reverse(TeamS);
        teamIPage.setRecords(TeamS);
        teamIPage.setTotal(records.size());
        return teamIPage;
    }

    @Override
    public IPage<Team> pageConditionVO(SearchPageDTO searchPageDTO) {

        IPage<Team> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
        LambdaQueryWrapper<Team> wrapper = Wrappers.lambdaQuery(Team.class);
        IPage<Team> teamIPage = baseMapper.selectPage(page, wrapper);
//        IPage<PaperVO> taskVOIPage = iBeanService.copyPageProperties(paperIPage, PaperVO.class);
        List<Team> records = teamIPage.getRecords();
        List<Team> TeamS = new ArrayList<>();

        int count = 1;
        int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum()-1)+1;
        int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
//        records.forEach(record->{
//            if (count >= start && count <= end){
//                PaperS.add(this.getPaper(record.getPaperID()));
//            }
//        });
        Collections.reverse(records);
        for (Team record:records) {
            if (count >= start && count <= end){
                TeamS.add(this.getTeamById(record.getTeamID()));
            }
            count += 1;
        }


//        Collections.reverse(TeamS);
        teamIPage.setRecords(TeamS);
        teamIPage.setTotal(records.size());
        return teamIPage;
    }
}
